
import { _decorator, Component, Label } from 'cc';
import i18n from './LanguageData';
const { ccclass, property, executeInEditMode, menu } = _decorator;

@ccclass('LocalizedLabel')
@menu('i18n/LocalizedLabel')
@executeInEditMode
export class LocalizedLabel extends Component {
	label: Label | null = null;

	@property({ visible: false })
		key: string = '';

	@property({ displayName: 'Key', visible: true, tooltip: "key&pam1,pam2" })
	get _key() {
		return this.key;
	}

	set _key(str: string) {
		this.updateLabel();
		this.key = str;
	}

	onLoad() {
		if (!i18n.ready) {
			i18n.init('zh');
		}

		this.fetchRender();
	}

	fetchRender() {
		const label = this.getComponent('cc.Label') as Label;

		if (label) {
			this.label = label;
			this.updateLabel();

			return;
		}
	}

	updateLabel() {
		this.label && (this.label.string = i18n.t(this.key));
	}
}
